package com.bjpowernode.services.impls;

import cn.hutool.core.util.IdUtil;
import com.bjpowernode.constants.Constant;
import com.bjpowernode.entities.TTran;
import com.bjpowernode.mappers.TTranMapper;
import com.bjpowernode.querys.BaseQuery;
import com.bjpowernode.querys.TranQuery;
import com.bjpowernode.services.TranService;
import com.bjpowernode.utils.LoginInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TranServiceImpl implements TranService {
    @Resource
    private TTranMapper tTranMapper;

    @Override
    public PageInfo<TTran> getTransByPage(Integer current) {
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        List<TTran> list = tTranMapper.selectByPage(new BaseQuery());
        return new PageInfo<>(list);
    }

    @Override
    public int addTran(TranQuery tranQuery) {
        TTran tTran = new TTran();
        BeanUtils.copyProperties(tranQuery, tTran);
        tTran.setTranNo(String.valueOf(IdUtil.getSnowflakeNextId()));
        tTran.setCreateTime(new Date());
        tTran.setCreateBy(LoginInfoUtil.getCurrentLoginUser().getId());
        return tTranMapper.insertSelective(tTran);
    }

    @Override
    public TTran getTranById(Integer id) {
        return tTranMapper.selectById(id);
    }
}
